package com.yykt.weshare.util;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

public class RestTemplateUtils {

    /**
     * 获取restTemplate，设置超时时间，避免网络问题
     */
    public static RestTemplate getRestTemplate(int connectTimeout, int readTimeout) {
        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        httpClientBuilder.setMaxConnPerRoute(600);
        httpClientBuilder.setMaxConnTotal(1200);

        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);
        factory.setConnectTimeout(connectTimeout);
        factory.setReadTimeout(readTimeout);
        RestTemplate restTemplate = new RestTemplate(factory);
        return restTemplate;
    }

}
